// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import App from './App'
import router from './router'

// import XLSX from 'xlsx'
Vue.prototype.$XLSX = XLSX

import store from "./store/index"

// import JSONBIG from 'json-bigint'
// axios.defaults.transformResponse = [
//   function (data) {
//     const json = JSONBIG({
//       storeAsString: true
//     })
//     const res = json.parse(data)
//     return res
//   }
// ]

import filters from '@/assets/js/filters.js'
Vue.prototype.filters = filters;

import dayjs from "dayjs";
import weekOfYear from "dayjs/plugin/weekOfYear";
import weekday  from "dayjs/plugin/weekday";
import "dayjs/locale/zh-cn";
Vue.prototype.dayjs = dayjs
dayjs.extend(weekOfYear);
dayjs.extend(weekday);
dayjs.locale("zh-cn");
// tk本地地址
// Vue.prototype.$tiktokUrl = 'http://www.jcmamorphling.com/';
// tk线上地址
Vue.prototype.$tiktokUrl = 'https://xianggang.jcmasz.com/';
// axios 引入
// 测试地址钟辉
// axios.defaults.baseURL = 'http://morphlingv4.ilzy.cn/'
// 测试地址润中
// axios.defaults.baseURL = 'http://www.jcmamorphling.com/'
// 线上地址
axios.defaults.baseURL = 'https://morphlingv4.jcmasz.com/'
Vue.prototype.$http = axios
// http request 拦截器
// let pending = [];
// const CancelToken = axios.CancelToken;
// const removePending = (config) => {
//   for (let p in pending) {
//     if (pending[p].url === config.url + '&' + config.method) {
//         pending[p].cancel("request canceled");
//         pending.splice(p, 1);
//     }
//   }
// }

axios.interceptors.request.use(
  config => {
    const token = window.localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = token;
    }
    // removePending(config);
    // config.cancelToken = new CancelToken((c) => {
    //     pending.push({ cancel: c, url: config.url + '&' + config.method });
    // });
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

// http response 拦截器
axios.interceptors.response.use(
  response => {
    // removePending(response.config);
    if (response.data.code == 100) {
      Vue.prototype.$message.error('登录失效!');
      router.replace('/login');
    } else if (response.data.code == 800001) {
      return response
    }
    return response;
  },
  error => {
    if (error && error.response && error.response.status) {
      if (error.response.status != 200) {
        Vue.prototype.$message.error('服务器连接失败！');
      }
    }
    return Promise.reject(error);
  }
);

// 定义全局自定义指令  
Vue.directive('has-permission', {  
  inserted: function (el) {  
    // 获取用户的权限列表
    const userRole = "17";
    const userId = [10000]
    let {role,id} = JSON.parse(window.localStorage.getItem("userinfo"));
    let Nrole;
    if(role){
     Nrole = role.split(',');
    }else{
      Nrole = []
    }
    // 检查用户是否有所需的权限
    if(!userId.includes(id)){
      if(!Nrole.includes(userRole)){
        el.style.display = "none";
      }
    }
  }
});
Vue.directive('has-pointerEvents', {  
  inserted: function (el) {  
    // 获取用户的权限列表
    const userRole = "17";
    const userId = [10000]
    let {role,id} = JSON.parse(window.localStorage.getItem("userinfo"));
    let Nrole;
    if(role){
     Nrole = role.split(',');
    }else{
      Nrole = []
    }
    // 检查用户是否有所需的权限
    if(!userId.includes(id)){
      if(!Nrole.includes(userRole)){
        el.style.pointerEvents = "none"
      }
    }
  }
});

// echarts 引入
Vue.prototype.$echarts = echarts


// bus 引入
import VueBus from 'vue-bus';

// 初始化样式
import '@/assets/css/reset.css';
import '@/assets/css/theme-chalk/index.css';
import '@/assets/css/common.css';
import '@/assets/css/main.css';
import '@/assets/css/font.css';

Vue.use(ELEMENT);
// Vue.use(ELEMENT, { size: 'small' });
Vue.use(VueBus);

Vue.config.productionTip = false


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {
    App
  },
  template: '<App/>'
})
